<template>
  <el-dialog
      :title="!dataForm.id ? '新增' : '修改'"
      :close-on-click-modal="false"
      :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="auto">
      <el-form-item label="公告标题" prop="title">
        <el-input v-model="dataForm.title" placeholder="公告标题"></el-input>
      </el-form-item>
      <el-form-item label="公告内容" prop="content">
        <el-input type="textarea" v-model="dataForm.content" placeholder="公告内容"></el-input>
      </el-form-item>
      <el-form-item label="公告类型" prop="type">
        <el-select v-model="dataForm.type" clearable allow-create filterable
                   default-first-option placeholder="可自行创建">
          <el-option label="消防安全培训" value="消防安全培训"></el-option>
          <el-option label="设备检修通知" value="设备检修通知"></el-option>
          <el-option label="火警演练" value="火警演练"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item class="center" label="公告重要等级" prop="level" style="vertical-align: middle">
        <div style="display: flex; align-items: center; height: 40px;">
          <el-rate v-model="dataForm.level" placeholder="公告重要等级"></el-rate>
        </div>
      </el-form-item>
      <el-form-item label="公告目标受众" prop="target">
        <el-select v-model="dataForm.target" clearable filterable default-first-option>
          <el-option label="所有" :value="0"></el-option>
          <el-option label="业主" :value="-1"></el-option>
          <el-option label="安全检查员" :value="-2"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        title: '',
        content: '',
        publishDate: '',
        type: '',
        level: 0,
        target: ''
      },
      dataRule: {
        title: [
          {required: true, message: '公告标题不能为空', trigger: 'blur'}
        ],
        content: [
          {required: true, message: '公告内容不能为空', trigger: 'blur'}
        ],
        type: [
          {required: true, message: '公告类型，如消防安全培训、设备检修通知、火警演练等不能为空', trigger: 'blur'}
        ],
        level: [
          {required: true, message: '公告重要等级不能为空', trigger: 'blur'}
        ],
        target: [
          {required: true, message: '公告目标受众不能为空', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$request({
            url: `/notice/notice/info/${this.dataForm.id}`,
            method: 'get',
            params: {}
          }).then((res) => {
            if (res && res.code === 200) {
              this.dataForm.title = res.notice.title
              this.dataForm.content = res.notice.content
              this.dataForm.publishDate = res.notice.publishDate
              this.dataForm.type = res.notice.type
              this.dataForm.level = res.notice.level
              this.dataForm.target = res.notice.target
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$request({
            url: `/notice/notice/${!this.dataForm.id ? 'save' : 'update'}`,
            method: 'post',
            data: {
              'id': this.dataForm.id || undefined,
              'title': this.dataForm.title,
              'content': this.dataForm.content,
              'publishDate': this.dataForm.publishDate,
              'type': this.dataForm.type,
              'level': this.dataForm.level,
              'target': this.dataForm.target
            }
          }).then((res) => {
            if (res && res.code === 200) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(res.msg)
            }
          })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
